package bas.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import bas.system.Util;

public class FileIO {

    public static boolean joinMember(String id, String pw, String name, String regId, String addr)
    {
        BufferedWriter writer = null;
        File f = new File(Util.getMemberDirPath());

        if (!f.exists())
        {
            f.mkdir();
        }

        try
        {
            writer = new BufferedWriter(new FileWriter(new File(f, id + ".txt")));
            StringBuffer sb = new StringBuffer();
            sb.append(id);
            sb.append("//");
            sb.append(pw);
            sb.append("//");
            sb.append(name);
            sb.append("//");
            sb.append(regId);
            sb.append("//");
            sb.append(addr);
            sb.append("//");
            sb.append("000000");
            sb.append("//");
            sb.append("b" + 0);
            writer.write(sb.toString());
            writer.close();
        }
        catch (IOException e)
        {
            System.out.println("Writing Error");
        }
        return true;
    }

    public static void saveRequestList(String id, boolean isCreation)
    {
        BufferedWriter writer = null;
        File f = null;
        String filename;

        if (isCreation)
        {
            f = new File(Util.getCreationDirPath());
            filename = Util.getCreationFileName();
        }
        else
        {
            f = new File(Util.getDeletionDirPath());
            filename = Util.getDeletionFileName();
        }

        if (!f.exists())
        {
            f.mkdir();
        }

        try
        {
            writer = new BufferedWriter(new FileWriter(new File(f, filename + ".txt"), true));
            writer.write(id.toString());
            writer.write("//");
            writer.close();
        }
        catch (IOException e)
        {
            System.out.println("Writing Error");
        }
    }

    public static void initAdmin()
    {
        BufferedWriter writer = null;
        File f = new File(Util.getAdminDirPath());

        if (!f.exists())
        {
            f.mkdir();
        }

        try
        {
            writer = new BufferedWriter(new FileWriter(new File(f, "admin.txt")));
            writer.write("admin//1234");
            writer.close();
        }
        catch (IOException e)
        {
            ;
        }
    }

    public static String[] get(String id) 
    {
        BufferedReader reader = null;
        String[] splitText = null;

        try
        {
            reader = new BufferedReader(new FileReader(new File(Util.getMemberDirPath(), id + ".txt")));
            String str = null;
            while ((str = reader.readLine()) != null)
            {
                splitText = str.split("//");
            }
            reader.close();
        }
        catch (IOException e)
        {
            ;
        }
        return splitText;
    }

    public static String getPw(String id)
    {
        BufferedReader reader = null;
        String pw = null;
        String path = null;
        String fileName = null;

        if (id.equals("admin"))
        {
            path = Util.getAdminDirPath();
            fileName = "admin";
        } 
        else
        {
            path = Util.getMemberDirPath();
            fileName = id;
        }

        try
        {
            reader = new BufferedReader(new FileReader(new File(path, fileName+ ".txt")));
            String str = null;

            while ((str = reader.readLine()) != null)
            {
                String[] splitText = str.split("//");
                pw = splitText[1];
            }
            reader.close();
        }
        catch (IOException e)
        {
            ;
        }
        return pw;
    }

    public static void setLatestAccNum(int accNum)
    {
        BufferedWriter writer = null;
        File f = new File(Util.getAdminDirPath());

        if (!f.exists())
        {
            f.mkdir();
        }

        try
        {
            writer = new BufferedWriter(new FileWriter(new File(f, "latest.txt")));
            writer.write(Integer.toString(accNum));
            writer.close();
        }
        catch (IOException e)
        {
            ;
        }
    }

    private static int getLatestAccNum()
    {
        BufferedReader reader = null;
        int accNum = 100000;

        try
        {
            reader = new BufferedReader(new FileReader(new File(
                    Util.getAdminDirPath(), "latest.txt")));
            String str = null;

            while ((str = reader.readLine()) != null)
            {
                accNum = Integer.parseInt(str);
            }
            reader.close();
        }
        catch (IOException e)
        {
            ;
        }
        return accNum;
    }

    public static void setAccNum(String id)
    {
        BufferedReader reader = null;
        int accNum = getLatestAccNum();
        accNum += 1;

        try
        {
            reader = new BufferedReader(new FileReader(new File(Util.getMemberDirPath(), id + ".txt")));
            String str = null;

            while ((str = reader.readLine()) != null)
            {
                String[] splitText = str.split("//");
                str.replace(splitText[6], new String(Integer.toString(accNum)));
            }
            reader.close();
        }
        catch (IOException e)
        {
            ;
        }
        setLatestAccNum(accNum);
    }

    public static void deleteAcc(String id)
    {
        File file = new File(Util.getMemberDirPath(), id + ".txt");
        file.delete();
    }

    public static String[] getRequestList(boolean isCreation)
    {
        BufferedReader reader = null;
        String[] list = null;
        String path;
        String fileName;

        if (isCreation)
        {
            path = Util.getCreationDirPath();
            fileName = Util.getCreationFileName();
        }
        else
        {
            path = Util.getDeletionDirPath();
            fileName = Util.getDeletionFileName();
        }

        try
        {
            reader = new BufferedReader(new FileReader(new File(path, fileName + ".txt")));
            String str = null;

            while ((str = reader.readLine()) != null)
            {
                list = str.split("//");
            }
            reader.close();
        }
        catch (IOException e)
        {
            ;
        }
        return list;
    }
}
